{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9b3f9a49-79cb-4028-8440-a331dafcc40a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b401b91a-cd85-4a6e-8c70-1be14770e038",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.5843, 0.1137, 0.3108, 0.4373, 0.9492])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randn(5)  # normal distrobution\n",
    "torch.rand(5)  # random number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "29883ae9-bc2b-4612-91ef-5215d9a8c645",
   "metadata": {},
   "outputs": [],
   "source": [
    "N = 20\n",
    "X = torch.rand(N)  # 生成n个随机数,0-1\n",
    "k = 2; b = 3\n",
    "Y = k * X + b + torch.randn(N) * 0.02  # 噪声用randn，正态分布"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0b2d686d-aff1-40e0-8e6c-34a7e0fd01b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x26844186b50>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD4CAYAAADiry33AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAXq0lEQVR4nO3dbYxc1X3H8e+PZSkLJWwUNgR2cexW1AmBgOnUkDol2CXY5iE4iBdOCVGjRCs3oUKp6gT3BWlaVaZyX9CkJMhFqImiBKHE3rg8GSSHkoQaPJs1GAKOXEKC15G8PCyJySrY5t8XcxeGYR7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DuwOiJebFHb5kqaPheAO5OQPwW4TNKRiBhpSQvnRtp/4y9ExKvAq5IeBs4FujXo0/T508DNURq83ifpF8D7gMda08S2yCTLunHoJs32hLW2NuxmDfstaQGwBbiui6/syjXsc0QsioiFEbEQ+B7wuS4PeUj3b/wHwF9IOlbSCcAFwNMtbmeW0vT5V5T+DwZJpwKLgWdb2srWyyTLuu6KPmpsT5hma8NulrLfNwHvAr6eXOEeiS5e9S9ln3MnTb8j4mlJ9wNPAK8Dt0dE1Sl63SDl3/U/A/8laQ+lIY0vRURXL18s6bvAxcApkvYDXwZ6Idss8xIIZmY5141DN2Zm1gQHvZlZzjnozcxyzkFvZpZzDnozs5xz0JuZ5ZyD3sws5/4fryPCNio/7SMAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X, Y)  # 快速画图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d11ded1d-af78-43fe-8f45-8c4443a92376",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([0.7030], requires_grad=True), tensor([0.1403], requires_grad=True))"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w_k = torch.rand(1)\n",
    "w_k.requires_grad = True\n",
    "w_b = torch.rand(1)\n",
    "w_b.requires_grad = True\n",
    "w_k, w_b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d4f7c4f3-5d68-4d51-97a9-d783c894e3c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x268441d6790>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWoAAAD4CAYAAADFAawfAAAAOXRFWHRTb2Z0d2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1Us+vlq+ovUZ/azP7tfLjn1HpBMD9kt6QdF6lv5E7VsI5/4GknZL+rLzCXPIO7jyWcM5dJcmc3f2YmX1V0suSViQ97u6xx7w6QcI/5z+U9Fkze0WlssDD7t6x7U/N7POS7pY0aWZvSvqEpAEpu/ziCjkABI6biQAQOIIaAAJHUANA4AhqAAgcQQ0AgSOoASBwBDUABO7/AWrcRheNJQ2lAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "test_x = torch.linspace(0, 1, 100)\n",
    "test_y = test_x * w_k.detach() + w_b.detach()  # .detach() == .data\n",
    "plt.plot(test_x, test_y)\n",
    "plt.scatter(X, Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1c901559-d973-4e05-af25-9c4816a93dcb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(12.8948, grad_fn=<MeanBackward0>)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_hat = w_k * X + w_b\n",
    "loss = ((y_hat - Y)**2).mean()  # 求数组平均值\n",
    "loss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "887726b5-4c53-4c70-a728-a8effe1fdde5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.7986, 0.7787, 0.2328, 0.6202, 0.4290, 0.5081, 0.2174, 0.3958, 0.8370,\n",
       "        0.4546, 0.4928, 0.6930, 0.7724, 0.5982, 0.1407, 0.7358, 0.3756, 0.6637,\n",
       "        0.4174, 0.4387], grad_fn=<AddBackward0>)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_hat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a940c0ba-64c6-47db-9071-9e674abfdab3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(None, None)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w_k.grad, w_b.grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1d50ff88-44b7-49a3-81fb-8830ee83c1df",
   "metadata": {},
   "outputs": [],
   "source": [
    "loss.backward()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d88547e3-4165-412d-bd01-0aa34dd1e939",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([0.7030], requires_grad=True), tensor([0.1403], requires_grad=True))"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w_k, w_b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7b61c80c-1f9b-4f54-a5e2-2ce6c9969412",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([-4.1674]), tensor([-7.1455]))"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w_k.grad, w_b.grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "deb7bbfd-e4b1-4369-b8ee-fd1dc7c81197",
   "metadata": {},
   "outputs": [],
   "source": [
    "lr = 0.05"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "eab71a01-3a90-4d13-9508-8dc1cab69eb2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([0.7030], requires_grad=True), tensor([0.1403], requires_grad=True))"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w_k, w_b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "78d160a4-56bf-4a7b-bda4-2a4ab273ce49",
   "metadata": {},
   "outputs": [],
   "source": [
    "w_k.data = w_k.data - lr * w_k.grad.data\n",
    "w_b.data = w_b.data - lr * w_b.grad.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5aab5e16-46d2-4b1e-ac20-f6dc93282ee5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([0.9114], requires_grad=True), tensor([0.4975], requires_grad=True))"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w_k, w_b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "112611d3-ba92-4afb-9328-1d346cacfbd4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x26844331fa0>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWoAAAD4CAYAAADFAawfAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAUmElEQVR4nO3de2xcaXnH8d/j+4xz8Ywzm03smXi5bLilIVsD2y6XZaEbWKBsV0i9CSRUKap6EZXaLdA/4I/+sVRILapohSJAFLUFVbBs6QW2SHS7pbC0DrvsAtutthRyhTiJnZvHiT1++seMxzPjsefYnjPzzvj7kaI48cF5X5L95c1znvMcc3cBAMLV0+4FAADWR1ADQOAIagAIHEENAIEjqAEgcH1xfNE9e/b4xMREHF8aALrSiRMnLrh7pt7nYgnqiYkJTU1NxfGlAaArmdmP1/ocpQ8ACBxBDQCBI6gBIHAENQAELtLNRDP7kaSrkgqSFt19Ms5FAQBWbKTr443ufiG2lQBAB3nkyTP66KPP6exsXvtHEnrw6EHdf2Qsll8rlvY8AOhmjzx5Rh98+BnlFwqSpDOzeX3w4WckKZawjlqjdkn/YmYnzOxYvQvM7JiZT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Iv89/LOkzZvaMimWB97t7x44/NbPPSbpb0h4zOy3pw5L6pfjyi0fIASBwPJkIAIEjqAEgcAQ1AASOoAaAwBHUABA4ghoAAkdQA0Dg/h/xVmKswJpwKgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "test_x = torch.linspace(0, 1, 100)\n",
    "test_y = test_x * w_k.detach() + w_b.detach()\n",
    "plt.plot(test_x, test_y)\n",
    "plt.scatter(X, Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2735d27f-206d-4aac-b940-4777562aa8bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "w_b.grad.data = torch.zeros(1)\n",
    "w_k.grad.data = torch.zeros(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "80776b19-b188-4af3-aaa1-2bd2aa2e4a1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([0.9114], requires_grad=True), tensor([0.4975], requires_grad=True))"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w_k, w_b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "17e959b5-db30-4a59-b6b8-2f591d69848d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([0.]), tensor([0.]))"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w_k.grad, w_b.grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7a275e7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "lr = 0.02\n",
    "for i in range(10):\n",
    "    # clear grad\n",
    "    # calc y_hat\n",
    "    # calc loss\n",
    "    # backward\n",
    "    # update grad\n",
    "    # update w_k, w_b\n",
    "    pass"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
